/**************************************************************
 *
 * userprog/ksyscall.h
 *
 * implements for systemcalls
 *
 * by Marcus Voelp  (c) Universitaet Karlsruhe & yafeng (c) HUST
 *
 *************************************************************/

#include "main.h"
#include "ksyscall.h"
#include "kernel.h"
#include "filesys.h"

void SysHalt() {
	kernel->interrupt->Halt();
}

int SysAdd(int op1, int op2) {
	return op1 + op2;
}

int SysStartProcess(char *filename) {
	OpenFile *executable = kernel->fileSystem->Open(filename);
	AddrSpace *space;
	if (executable == NULL) {
		printf("Unable to open file %s\n", filename);
		return -1;
	}
	space = new AddrSpace(executable);
	space->Load(filename);
	kernel->currentThread->space = space;
	delete executable; // close file
	space->InitRegisters(); // set the initial register values
	space->RestoreState(); // load page table register
	kernel->machine->Run(); // jump to the user progam
	ASSERT(FALSE); // machine->Run never returns;
	// the address space exits
	// by doing the syscall "exit"
	return 0;
}
